package util;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;

import fire.Main;
import fire.chacracter.Player;
import fire.npc.NPC;
import fire.object.TerrainObject;

import jgame.JGObject;

public class OOMLoad {
	public static Hashtable<String, Constructor<?>> objectContructor = new Hashtable<>();

	public static void oomLoad(String oomFile, ArrayList<JGObject> oomList,
			Main main) {
		int ln = 1;
		try {
			InputStreamReader ins = new InputStreamReader(new FileInputStream(
					oomFile));
			BufferedReader in = new BufferedReader(ins);
			String line;
			String[] fields;
			Constructor<?> con;
			while ((line = in.readLine()) != null) {
				fields = line.split("\t");
				if(fields.length==3){
				con = objectContructor.get(fields[0]);
				System.out.println(con+"  "+fields[0]);
				oomList.add((JGObject) con.newInstance(
						Double.parseDouble(fields[1]),
						Double.parseDouble(fields[2])));
				}
				ln++;
			}
		} catch (Exception e) {
			e.printStackTrace();
			System.err.println("error format in line " + ln + " in file "
					+ oomFile);
		}
	}

	public static Hashtable<String, Constructor<?>> loadClassList() {
		Hashtable<String, Constructor<?>> classList = new Hashtable<String, Constructor<?>>();
		try {
			InputStreamReader ins = new InputStreamReader(new FileInputStream(
					"setting/classlist.txt"));
			BufferedReader in = new BufferedReader(ins);
			String line;
			String[] fields;
			while ((line = in.readLine()) != null) {
				fields = line.split("\t");
				if(fields.length==2){
				Class<?> clas = Class.forName(fields[1]);
				Constructor<?> con = clas.getConstructor(double.class,
						double.class);
				classList.put(fields[0], con);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		objectContructor = classList;
		return classList;
	}

}
